package com.core.family.admin.controller;

import com.core.common.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.stereotype.Service;
import com.core.family.admin.domain.Opencode;
import com.core.family.admin.service.OpencodeService;
import com.core.family.admin.domain.req.OpencodeReq;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.common.annotation.MenuDesc;
import com.core.common.domain.response.R;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author honglei
 * @version 1.0
 * @time: 2024-6-18 15:51:11
 * @Description 服务实现层 开源项目表
 */
@MenuDesc(desc = "开源项目表", code = "ucOpencode")
@Api(tags = "OpencodeController 开源项目表")
@RestController
@RequestMapping("/api/ucOpencode/")
public class OpencodeController {

    @Autowired
    OpencodeService service;

    /**
     * 根据id获取详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping(value = "get/{id}")
    public R get(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.getById(id));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "getPage", method = {RequestMethod.GET, RequestMethod.POST})
    public R getPage(@RequestBody OpencodeReq queryReq) {
        return R.ok(service.selectPage(queryReq));
    }

    /**
     * 批量查询
     *
     * @return
     */
    @RequestMapping(value = "getList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getList(@RequestBody OpencodeReq queryReq) {
        return R.ok(service.selectList(queryReq));
    }

    /**
     * 新增
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "create")
    public R create(@RequestBody Opencode bean) {
        return R.ok(service.save(bean));
    }

    /**
     * 普通更新
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "edit")
    public R edit(@RequestBody Opencode bean) {
        return R.ok(service.updateById(bean));
    }

    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "del/{id}")
    public R del(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param request
     * @param ids
     * @return
     */
    @PostMapping(value = "delBatch")
    public R batchDel(HttpServletRequest request, @RequestBody List<String> ids) {
        return R.ok(service.removeByIds(ids));
    }


    @PostMapping(value = "simpleU")
    public R simpleU(String sourcePath, @RequestParam(required = false) String mark) {
        return R.ok(service.simple(sourcePath, mark, true));
    }



    @PostMapping(value = "getExcelTp")
    public void importExcel(HttpServletResponse response) {
        ExcelUtil.downloadXLSX(response, "开源项目表", new String[]{"link", "mark"}, null);
    }

    @PostMapping(value = "importExcel")
    public R importExcel(HttpServletResponse response, MultipartFile file) {
        List<Map<String, Object>> mapList = ExcelUtil.parse(file, null);
        for (Map<String, Object> map : mapList) {
            service.simple(map.get("link").toString(), map.get("mark").toString(), true);
        }
        return R.ok();
    }


}
